package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOrdinaryCargoPublishCostBinding;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.utils.CustomTextWatcher;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.RangeFocusChangeListener;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.viewmodel.transport.CargoPublishCostViewModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2021/6/22 11:10
 * @Description: 普通货源发布填写费用信息页面
 */
public class OrdinaryCargoPublishCostActivity extends AppActivity {

    @Inject
    CargoPublishCostViewModel viewModel;
    ActivityOrdinaryCargoPublishCostBinding binding;
    //数据实体类
    private CargoPublishEntity mEntity;

    public static void start(Activity activity, CargoPublishEntity entity, int requestCode) {
        Intent intent = new Intent(activity, OrdinaryCargoPublishCostActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        viewModel = new ViewModelProvider(this).get(CargoPublishCostViewModel.class);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_ordinary_cargo_publish_cost);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("费用要求");
        initEvent();
        initData();
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
        //设置输入范围，失去焦点后自动补全
        binding.etReleaseCargoMsgPrice.setOnFocusChangeListener(new RangeFocusChangeListener(0, 1000));
        binding.etReleaseCargoFreight.setOnFocusChangeListener(new RangeFocusChangeListener(10, 50000));
        //监听运费
        binding.etReleaseCargoFreight.addTextChangedListener(new CustomTextWatcher() {
            @Override
            protected void delayedChanged(Editable content) {
                //设置运费
                mEntity.setFreight(NumberUtil.convertToDouble(content.toString()));
                adjustFreight();
            }
        });
        binding.rgReleaseCargoType.setOnCheckedChangeListener((group, checkedId) -> {
            //设置运费类型
            mEntity.setFreightType(checkedId == R.id.rb_release_cargo_one_price ? 0 : 1);
            if (mEntity.getFreightType() == 0) {
                binding.llReleaseCargoFreightUnit.setVisibility(View.VISIBLE);
                binding.rbReleaseCargoFreightTip.setText(R.string.tip_freight_once);
            } else {
                binding.llReleaseCargoFreightUnit.setVisibility(View.GONE);
                binding.rbReleaseCargoFreightTip.setText(R.string.tip_freight_phone);
            }
            //重算总运费
            adjustFreight();
        });
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity();
        }
        if (mEntity.getFreightType() == 0 && mEntity.getFreightMode() == 0) {
            switchFreightMode(0);
        } else {
            syncData();
        }
    }

    private void syncData() {
        if (mEntity.getMsgPrice() != null) {
            binding.etReleaseCargoMsgPrice.setText(NumberUtil.trim0(mEntity.getMsgPrice()));
        }
        if (mEntity.getFreight() != 0) {
            binding.etReleaseCargoFreight.setText(NumberUtil.trim0(mEntity.getFreight()));
        }
        //运费类型
        if (mEntity.getFreightType() == 0) {
            binding.rgReleaseCargoType.check(R.id.rb_release_cargo_one_price);
        } else {
            binding.rgReleaseCargoType.check(R.id.rb_release_cargo_phone);
        }
        //判断是否是电议
        if (mEntity.getFreightType() == 1) {
            //重算总运费
            adjustFreight();
            //一口价默认选中趟
            switchFreightMode(0);
            return;
        }
        switch (mEntity.getFreightMode()) {
            case CargoPublishEntity.FreightMode.MODE_WEIGHT://吨
                switchFreightMode(1);
                break;
            case CargoPublishEntity.FreightMode.MODE_VOLUME://方
                switchFreightMode(2);
                break;
            case CargoPublishEntity.FreightMode.MODE_ONCE://趟
            default://默认趟
                switchFreightMode(0);
                break;

        }

    }

    @Override
    public void onClick(View v) {
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.tvReleaseCargoOnce) {
            //防止重复点击
            if (binding.tvReleaseCargoOnce.isSelected()) {
                return;
            }
            switchFreightMode(0);
        } else if (v == binding.tvReleaseCargoWeight) {
            //防止重复点击
            if (binding.tvReleaseCargoWeight.isSelected()) {
                return;
            }
            //判断是否填写货重
            if (mEntity.getGoodsWeight() == 0) {
                MToast.showToast(this, "请先至货物信息中填写货重");
                return;
            }
            switchFreightMode(1);
        } else if (v == binding.tvReleaseCargoVolume) {
            //防止重复点击
            if (binding.tvReleaseCargoVolume.isSelected()) {
                return;
            }
            //判断是否填写体积
            if (mEntity.getGoodsVolume() == 0) {
                MToast.showToast(this, "请先至货物信息中填写货物体积");
                return;
            }
            switchFreightMode(2);
        } else if (v == binding.tvReleaseCargoSubmit) {
            clearAllFocus();
            checkData();
        }
    }

    /**
     * 切换运费方式
     */
    private void switchFreightMode(int position) {
        int count = binding.llReleaseCargoFreightType.getChildCount();
        for (int i = 0; i < count; i++) {
            binding.llReleaseCargoFreightType.getChildAt(i).setSelected(false);
        }
        binding.llReleaseCargoFreightType.getChildAt(position).setSelected(true);
        //设置运费方式
        switch (position) {
            case 0:
                mEntity.setFreightMode(CargoPublishEntity.FreightMode.MODE_ONCE);
                binding.tvReleaseCargoFreightSuffix.setText("元/趟");
                break;
            case 1:
                mEntity.setFreightMode(CargoPublishEntity.FreightMode.MODE_WEIGHT);
                binding.tvReleaseCargoFreightSuffix.setText("元/吨");
                break;
            case 2:
                mEntity.setFreightMode(CargoPublishEntity.FreightMode.MODE_VOLUME);
                binding.tvReleaseCargoFreightSuffix.setText("元/方");
                break;
        }
        //重算总运费
        adjustFreight();
    }

    /**
     * 调整运费
     */
    private void adjustFreight() {
        //设置普通货源运费展示
        if (mEntity.getFreightType() == 1 || mEntity.getFreight() == 0) {
            binding.llReleaseCargoFreight.setVisibility(View.GONE);
            return;
        }
        binding.llReleaseCargoFreight.setVisibility(View.VISIBLE);
        //根据运费类型计算运费
        switch (mEntity.getFreightMode()) {
            case CargoPublishEntity.FreightMode.MODE_ONCE:
                binding.tvReleaseCargoFreightTotal.setText(String.format("%s元", NumberUtil.trim0(mEntity.getFreight())));
                break;
            case CargoPublishEntity.FreightMode.MODE_WEIGHT:
                binding.tvReleaseCargoFreightTotal.setText(String.format("%s元", NumberUtil.trim0(mEntity.getGoodsWeight() * mEntity.getFreight())));
                break;
            case CargoPublishEntity.FreightMode.MODE_VOLUME:
                binding.tvReleaseCargoFreightTotal.setText(String.format("%s元", NumberUtil.trim0(mEntity.getGoodsVolume() * mEntity.getFreight())));
                break;
        }
    }

    /**
     * 检查数据并回传
     */
    private void checkData() {
        if (mEntity == null) return;
        String msgPrice = binding.etReleaseCargoMsgPrice.getText().toString();
        String freight = binding.etReleaseCargoFreight.getText().toString();
        //根据运费类型判断
        if (mEntity.getFreightType() == 0) {
            if (TextUtils.isEmpty(freight)) {
                MToast.showToast(this, "请输入运费单价");
                return;
            }
            //设置运费
            mEntity.setFreight(NumberUtil.convertToDouble(freight));
        }
        //设置信息费
        if (TextUtils.isEmpty(msgPrice)) {
            mEntity.setMsgPrice(null);
        } else {
            mEntity.setMsgPrice(NumberUtil.convertToDouble(msgPrice));
        }
        //修改运费展示
        adjustFreight();
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
